﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Yuebon.Commons.Json;
using Yuebon.Commons.Models;
using Yuebon.Security.Models;
using Yuebon.WebApi.Common;

namespace Yuebon.WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Produces("application/json")]
    [ApiController]
    public class BaseController : Controller
    {
        /// <summary>
        /// 当前登录的用户属性
        /// </summary>
        public User CurrentUser;


        /// <summary>
        /// 把object对象转换为ContentResult
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ToJsonContent")]
        protected IActionResult ToJsonContent(object obj)
        {
            string result = JsonConvert.SerializeObject(obj, Formatting.Indented);
            return Content(obj.ToJson());
        }


        /// <summary>
        /// 验证token的合法性。如果不合法，返回MyApiException异常
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        [HttpPost("CheckToken")]
        [HiddenApi]
        public CommonResult CheckToken(string token)
        {

            CommonResult result = new CommonResult();
            TokenProvider tokenProvider = new TokenProvider();
            result = tokenProvider.ValidateToken(token);

            return result;
        }
    }
}